// 系统菜单-权限表 - System
import { DataTypes, UUIDV4 } from 'sequelize';
import { MySqlModel, getSequelize } from '../db';
import { generatePermissionTree } from '../utils';

class SysMenus extends MySqlModel {
  static async getMenuRouters(tree) {
    const routers = await this.findAll({ where: { disabled: false }, order: [['sequence', 'ASC'], ['code', 'ASC']] });
    generatePermissionTree(tree, routers.map((r) => r.toJSON()), 0);
  }

  static async getUserHotelMenuRouters(tree, user_id, hotel_id) {
    const routers = await this.findAll({
      where: { disabled: false },
      include: [{
        model: getSequelize().models.UserPermissions,
        as: 'user_permission',
        attributes: ['user_id', 'hotel_id'],
        required: true,
        where: {
          user_id,
          hotel_id
        }
      }],
      order: [['sequence', 'ASC'], ['code', 'ASC']]
    });

    // console.log(routers.length);
    generatePermissionTree(tree, routers.map((r) => r.toJSON()), 0);
  }
}

SysMenus.init({
  id: {
    type: DataTypes.UUID, unique: 'id', allowNull: false, defaultValue: UUIDV4, validate: { isUUID: 4 }, primaryKey: true, comment: '主键', editable: false
  },
  code: {
    type: DataTypes.INTEGER, unique: 'code', allowNull: false, comment: '菜单ID', editable: false
  },
  name: {
    type: DataTypes.STRING(50), allowNull: true, comment: '路由名字', logger: true
  },
  parent_code: {
    type: DataTypes.INTEGER, allowNull: true, comment: '父菜单ID'
  },
  redirect: {
    type: DataTypes.STRING(50), allowNull: true, comment: '重定向路由'
  },
  path: {
    type: DataTypes.STRING(200), allowNull: true, comment: '路由地址'
  },
  component: {
    type: DataTypes.STRING(200), allowNull: true, comment: '组件路径'
  },
  hidden: {
    type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false, comment: '是否隐藏侧边栏'
  },
  alwaysShow: {
    type: DataTypes.BOOLEAN, allowNull: true, defaultValue: false, comment: '一直显示根路由'
  },
  title: {
    type: DataTypes.STRING(50), allowNull: true, comment: '菜单名称'
  },
  icon: {
    type: DataTypes.STRING(50), allowNull: true, comment: '菜单图标'
  },
  noCache: {
    type: DataTypes.BOOLEAN, allowNull: true, comment: '不缓存组件'
  },
  breadcrumb: {
    type: DataTypes.BOOLEAN, allowNull: true, comment: '是否显示面包屑'
  },
  affix: {
    type: DataTypes.BOOLEAN, allowNull: true, comment: '是否固定页签'
  },
  noTagsView: {
    type: DataTypes.BOOLEAN, allowNull: true, comment: '不显示页签'
  },
  activeMenu: {
    type: DataTypes.STRING(50), allowNull: true, comment: '显示高亮的路由路径'
  },
  followAuth: {
    type: DataTypes.STRING(50), allowNull: true, comment: '权限依赖路由'
  },
  canTo: {
    type: DataTypes.BOOLEAN, allowNull: true, comment: '是否可跳转'
  },
  menu_type: {
    type: DataTypes.STRING(50), allowNull: true, comment: '菜单类型'
  },
  permissions: {
    type: DataTypes.STRING(100), allowNull: true, comment: '权限标识'
  },
  disabled: {
    type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false, comment: '禁用', logger: true
  },
  sequence: {
    type: DataTypes.INTEGER, allowNull: true, defaultValue: 1, validate: { min: 0, max: 999999 }, comment: '排序', logger: true
  },
  created_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '创建用户', editable: false
  },
  updated_id: {
    type: DataTypes.UUID, allowNull: false, validate: { isUUID: 4 }, comment: '更新用户', logger: true, editable: false, model: 'Users', column: 'login_name'
  },
  created_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE, allowNull: false, editable: false, comment: '更新时间'
  },
  version: {
    type: DataTypes.INTEGER, allowNull: false, defaultValue: 0, editable: false, comment: '版本控制'
  }
}, {
  sequelize: getSequelize(),
  tableName: 'sys_menus',
  comment: '系统菜单'
});

SysMenus.associate = (db) => {
  SysMenus.hasOne(db.UserPermissions, {
    foreignKey: 'permission_id',
    sourceKey: 'id',
    as: 'user_permission'
  });
};

SysMenus.beforeValidate(async (instance, options) => {
  if (instance.menu_type === 'F') {
    instance.activeMenu = null;
    instance.affix = false;
    instance.alwaysShow = false;
    instance.breadcrumb = false;
    instance.canTo = false;
    instance.component = null;
    instance.followAuth = null;
    instance.hidden = false;
    instance.noCache = true;
    instance.noTagsView = true;
    instance.path = null;
    instance.redirect = null;
  }
});

SysMenus.beforeDestroy(async (instance, options) => {
  throw new Error('系统数据，无法删除');
});

export default SysMenus;
